package com.alipay.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Create By IntelliJ IDEA
 *
 * @author: myq
 * @create-Time: 2017/12/7  8:53
 */
public class AlipayFactory {
    private static String SIGNTYPE = "RSA2";
    private static String CHARSET = "utf-8";
    private static String FORMAT = "json";

    private static String appId;
    private static String merchantPrivate;
    private static String alipayPublicKey;
    private static String returnUrl;
    private static String getewayUrl;

    static {
        InputStream stream = AlipayFactory.class.getClassLoader().getResourceAsStream("alipay.properties");
        Properties prop = new Properties();
        try {
            prop.load(stream);
            appId = (String) prop.get("app_id");
            merchantPrivate = (String) prop.get("merchant_private_key");
            alipayPublicKey = (String) prop.get("alipay_public_key");
            returnUrl = (String) prop.get("return_url");
            getewayUrl = (String) prop.get("getewayUrl");


        } catch (IOException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }


    public static AlipayClient  getClient(){
        return new DefaultAlipayClient(getewayUrl,appId,merchantPrivate,FORMAT,CHARSET,alipayPublicKey,SIGNTYPE);
    }

    public static void setReturn(AlipayTradePagePayRequest request) {
        request.setReturnUrl(returnUrl);
    }
}
